package net.xju3_4.controller;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.PaymentEntity;
import net.xju3_4.service.impl.merchantPaymentServiceImpl;
import net.xju3_4.service.merchantPaymentService;

import java.util.List;

@RequestMapping("/api/merchantPayment")
public class merchantPaymentController {

    merchantPaymentService merchantPaymentService = new merchantPaymentServiceImpl();

    //    分页查找
    @RequestMapping("/pageList.do")
    public void orderPageList(HttpServletRequest req, HttpServletResponse resp) {

        PaymentEntity paymentEntity = new PaymentEntity();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        String merchantId = req.getParameter("merchantId");
        String paymentType = req.getParameter("paymentType");
        String paymentTime = req.getParameter("paymentTime");
        String status = req.getParameter("status");
        String returnTime = req.getParameter("refundTime");
        String amount = req.getParameter("amount");
        String pageNumStr = req.getParameter("pageNum");
        int pageNum = pageNumStr == null ? 1 : Integer.parseInt(pageNumStr);
        String pageSizeStr = req.getParameter("pageSize");
        int pageSize = pageSizeStr == null ? 10 : Integer.parseInt(pageSizeStr);

        PageInfo<PaymentEntity> pageInfo = merchantPaymentService.PageList(paymentType, paymentTime,status,merchantId,returnTime,amount, pageNum, pageSize);
        if (pageInfo == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else{
            JSONUtil.printByJSON(resp, Result.success(pageInfo, "查询成功"));
        }
    }
    //    分页查找
    @RequestMapping("/selectAll.do")
    public void orderSelectAll(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        String merchantId = req.getParameter("merchantId");
        String month = req.getParameter("month");
        String year = req.getParameter("year");
       List< PaymentEntity> payment = merchantPaymentService.selectAll(month, year,merchantId);
       if (payment == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else{
            JSONUtil.printByJSON(resp, Result.success(payment, "查询成功"));
        }
    }

    //根据输入年月进行筛选查找
    @RequestMapping("/selectYear.do")
    public void selectYear(HttpServletRequest req, HttpServletResponse resp) {

        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        String merchantId = req.getParameter("merchantId");
        List < PaymentEntity> payment = merchantPaymentService.selectYear(merchantId);
        if (payment == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else {
            JSONUtil.printByJSON(resp, Result.success(payment, "查询成功"));
        }
    }
    //根据输入年月进行筛选查找
    @RequestMapping("/selectMonth.do")
    public void selectMonth(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        String merchantId = req.getParameter("merchantId");
        List < PaymentEntity> payment = merchantPaymentService.selectMonth(merchantId);
        if (payment == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else {
            JSONUtil.printByJSON(resp, Result.success(payment, "查询成功"));
        }
    }
}
